<?php

namespace app\admin\controller;

use app\common\controller\Backend;

/**
 * 控制台
 *
 * @icon fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{

    /**
     * 查看
     */
    public function index()
    {
        $seventtime = \fast\Date::unixtime('day', -7);

        //计算7天前数据
        $paylist = $createlist = $cashlist =[];
        for ($i = 0; $i < 7; $i++)
        {
            $day = date("Y-m-d", $seventtime + ($i * 86400));
            $dayp = date("Y-m-d", $seventtime + (($i+1) * 86400));

            $createlist[$day]   = db('User')->whereTime('createTime', 'between', [$day, $dayp])->count();
            $paylist[$day]      = db('cash')->whereTime('createTime', 'between', [$day, $dayp])->count();
            $cashlist[$day]     = db('cash')->whereTime('createTime', 'between', [$day, $dayp])->sum('money');
        }

        //计算用户总数
        $totaluser = model('User')->count();
        //总投资金额
        $totalviews = db('borrow_tender')->sum('account');
        //总投资笔数
        $totalorder = db('borrow_tender')->count();
        //平均投资金额
        $avgorderamount = db('borrow_tender')->avg('account');

        //今日注册总数
        $todayusersignup = db('User')->whereTime('createTime', 'today')->count();
        //今日登陆总数
        $todayuserlogin = db('User')->whereTime('createTime', 'today')->count();//TODO createTime 需要修改成最后登陆时间
        //今日投标数
        $todayorder = db('borrow_tender')->whereTime('createTime', 'today')->count();
        //今日投标金额
        $todayunsettleorder = db('borrow_tender')->whereTime('createTime', 'today')->sum('account');

        //本周新增
        $sevendnu = db('User')->whereTime('createTime', 'week')->count();
        //本周活跃
        $sevendau = db('User')->whereTime('createTime', 'week')->count();//TODO createTime 需要修改成最后登陆时间

        $this->view->assign([
            'totaluser'          => $totaluser,
            'totalviews'         => $totalviews,
            'totalorder'         => $totalorder,
            'avgorderamount'     => round($avgorderamount,2),
            'todayuserlogin'     => $todayuserlogin,
            'todayusersignup'    => $todayusersignup,
            'todayorder'         => $todayorder,
            'todayunsettleorder' => empty($todayunsettleorder)?'0':$todayunsettleorder,
            'sevendnu'           => $sevendnu,
            'sevendau'           => $sevendau,
            'paylist'            => $paylist,
            'createlist'         => $createlist,
            'cashlist'           => $cashlist,
        ]);


        return $this->view->fetch();
    }

}
